<?php

namespace app\controller\system;

use app\model\system\SysRole;
use app\controller\BaseController;
use app\service\Logger;
use support\Request;
use support\Response;

class RoleController extends BaseController
{

    public function create(Request $request): Response
    {
        $data = $request->post();
        $user = SysRole::create($data);
        if (!$user) {
            return $this->error('创建失败');
        } else {
            return $this->success([]);
        }
    }

    public function form(int $id): Response
    {
        $log = SysRole::where('id', $id)->find();
        if (!$log) {
            return $this->error('数据不存在');
        }
        return $this->success($log->getFormArray());
    }


    public function update(Request $request, int $id): Response
    {
        $data = $request->post();
        $user = SysRole::find($id);

        if (!$user) {
            return $this->error('数据不存在');
        }

        if ($user->update($data)) {
            return $this->success($user->getFormArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function delete(int $id): Response
    {
        $role = SysRole::find($id);
        if (!$role) {
            return $this->error('数据不存在');
        }
        if (!$role->delete()) {
            return $this->error('删除失败');
        } else {
            return $this->success(["id" => $id]);
        }
    }

    public function page(Request $request): Response
    {
        $params = $request->get();
        extract($params);
        $roleModel = SysRole::order("id", "desc");
        if (isset($keywords) && $keywords) {
            $roleModel->whereLike("name|code", "%" . $keywords . "%");
        }
        $list = $roleModel->paginate(['list_rows' => $pageSize, 'page' => $pageNum]);
        $total = $list->total();
        return $this->success(["list" => $list->items(), "total" => $total]);
    }

    public function getOptions(): Response
    {
        $list = SysRole::where("status", 1)->column(["id" => "value", "name" => "label"]);
        return $this->success($list);
    }

    public function getMenuIds(int $roleId): Response
    {
        $role = SysRole::find($roleId);
        $list = $role->getMenuIds();
        return $this->success($list);
    }

    public function setStatus(Request $request, int $roleId): Response
    {
        $status = $request->post('status');
        $role = SysRole::find($roleId);
        $role->status = $status;
        if ($role->save()) {
            return $this->success($role->toArray());
        } else {
            return $this->error();
        }
    }

    public function setMenus(Request $request, int $roleId): Response
    {
        $menuIds = $request->post();
        $role = SysRole::find($roleId);
        $menus = [];
        foreach ($menuIds as $menuId) {
            $menus[] = (int) $menuId;
        }
        $role->menus()->saveAll($menus);
        if ($role->save()) {
            return $this->success($menus);
        } else {
            return $this->error();
        }
    }
}
